package com.wanxi.doctor.service.Impl;

import com.wanxi.doctor.mapper.DoctorMapper;
import com.wanxi.entity.Doctor;
import com.wanxi.feign.DoctorFeignClient;

import com.wanxi.entity.DoctorApply;
import com.wanxi.result.CommonResult;
import com.wanxi.vo.*;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "医生服务")
@RestController
@RequestMapping("/doctor")
public class DoctorServiceImpl implements DoctorFeignClient {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private DoctorMapper doctorMapper;

    @GetMapping("/findAllFront")
    @Override
    public CommonResult findAllFront(DoctorQuery doctorQuery) {
        if(doctorQuery.getPatientId() == null && doctorQuery.getProductId() != null){
            logger.info("入参",doctorQuery);
            List<DoctorAddDTO> doctors = doctorMapper.findAllFrontByProduct(doctorQuery);
            logger.info("出参",doctors);
            return CommonResult.success(doctors);
        }
        if (doctorQuery.getPatientId() != null && doctorQuery.getProductId() == null){
            logger.info("入参",doctorQuery);
            List<DoctorAddDTO> doctors = doctorMapper.findAllFrontByPaintent(doctorQuery);
            logger.info("出参",doctors);
            return CommonResult.success(doctors);
        }
        if(doctorQuery.getPatientId() != null && doctorQuery.getProductId() != null){
            logger.info("入参",doctorQuery);
            List<DoctorAddDTO> doctors = doctorMapper.findAllFrontByProductAndPaintent(doctorQuery);
            logger.info("出参",doctors);
            return CommonResult.success(doctors);
        }
            logger.info("入参",doctorQuery);
            List<DoctorAddDTO> doctors = doctorMapper.findAllFront(doctorQuery);
            logger.info("出参",doctors);
            return CommonResult.success(doctors);

    }

    @GetMapping("/findAllBack")
    @Override
    public CommonResult findAllBack(DoctorQuery doctorQuery) {
        if(doctorQuery.getPatientId() == null && doctorQuery.getProductId() != null){
            logger.info("入参",doctorQuery);
            List<DoctorAddDTO> doctors = doctorMapper.findAllFrontByProductBack(doctorQuery);
            logger.info("出参",doctors);
            return CommonResult.success(doctors);
        }
        if (doctorQuery.getPatientId() != null && doctorQuery.getProductId() == null){
            logger.info("入参",doctorQuery);
            List<DoctorAddDTO> doctors = doctorMapper.findAllFrontByPaintentBack(doctorQuery);
            logger.info("出参",doctors);
            return CommonResult.success(doctors);

        }
        if(doctorQuery.getPatientId() != null && doctorQuery.getProductId() != null){
            logger.info("入参",doctorQuery);
            List<DoctorAddDTO> doctors = doctorMapper.findAllFrontByProductAndPaintentBack(doctorQuery);
            logger.info("出参",doctors);
            return CommonResult.success(doctors);
        }
        logger.info("入参",doctorQuery);
        List<DoctorFindBack> doctors = doctorMapper.findAllBack(doctorQuery);
        logger.info("出参",doctors);
        return CommonResult.success(doctors);
    }

    @PostMapping("/add")
    @Override
    public CommonResult add(DoctorAddDTO doctorAddDTO) {
        logger.info("入参",doctorAddDTO);
        Integer i = doctorMapper.add(doctorAddDTO);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @PostMapping("/addApply")
    @Override
    public CommonResult addApply(DoctorApplyDTO doctorApplyDTO) {
        logger.info("入参",doctorApplyDTO);
        Integer i = doctorMapper.addApply(doctorApplyDTO);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @PutMapping("/updateAuthentication")
    @Override
    //更新医生认证信息
    public CommonResult updateAuthentication(Long id, DoctorAddDTO doctorAddDTO) {
        logger.info("入参",id,doctorAddDTO);
        Integer i = doctorMapper.updateAuthentication(id, doctorAddDTO);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @GetMapping("/findAccountStatus")
    @Override
    //查询医生入驻审核状态
    public CommonResult findAccountStatus(long id, Integer state) {
        logger.info("入参",id,state);
        Doctor doctor = doctorMapper.findAccountStatus(id, state);
        logger.info("出参",doctor);
        return CommonResult.success(doctor);
    }

    @PutMapping("/del")
    @Override
    public CommonResult delete(DoctorStatus doctorStatus) {
        logger.info("入参",doctorStatus);
        Integer i = doctorMapper.delete(doctorStatus);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @PutMapping("/updateStatus")
    @Override
    public CommonResult updateStatus(DoctorStatus doctorStatus) {
        logger.info("入参",doctorStatus);
        Integer i = doctorMapper.updateStatus(doctorStatus);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @PutMapping("/updateFailureReason")
    @Override
    //    更新失败原因
    public CommonResult updateFailureReason(Long id,String failureReason) {
        logger.info("入参",id,failureReason);
        Integer i = doctorMapper.updateFailureReason(id,failureReason);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @GetMapping("/findAccountApply")
    @Override
    public CommonResult findAccountApply(DoctorApplyQuery doctorApplyQuery) {
        logger.info("入参",doctorApplyQuery);
        List<DoctorApply> doctors = doctorMapper.findAccountApply(doctorApplyQuery);
        logger.info("出参",doctors);
        return CommonResult.success(doctors);
    }

    @GetMapping("/findAccountApplyFront")
    @Override
    public CommonResult findAccountApplyFront(DoctorApplyQuery doctorApplyQuery) {
        logger.info("入参",doctorApplyQuery);
        DoctorApplyDTO doctor = doctorMapper.findAccountApplyFront(doctorApplyQuery);
        logger.info("出参",doctor);
        return CommonResult.success(doctor);
    }

    @PostMapping("/addDoctorServer")
    @Override
    public CommonResult addDoctorServer(Long id, List<Long> pIds) {
        Integer i = 0;
        logger.info("入参",id,pIds);
        for (Long pId : pIds) {
            doctorMapper.addDoctorServer(id, pId);
            i++;
        }
        logger.info("出参",i);
//        Integer i = doctorMapper.addDoctorServer(id, pId);
        if (i > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }
}
